package com.shareyi.study.groovyscript.impl;

import com.shareyi.study.groovyscript.GroovyExecutor;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.util.Map;

/**
 * Created by david on 2017/3/11.
 */
public class GroovyShellExecutor implements GroovyExecutor {

    GroovyShell groovyShell=new GroovyShell();
    Script scriptLazy=null;

    public Object executeScript(String script, Map<String, Object> param) {
       // Binding bind=new Binding(param);
      //  GroovyShell groovyShell=new GroovyShell();
        Script script1=this.getScript(script);
        Binding binding=new Binding(param);
        script1.setBinding(binding);
        InvokerHelper ik=null;

        return script1.run();
       // return groovyShell.evaluate(script);
    }


    public Script getScript(String script){
        if(scriptLazy==null){
            scriptLazy=groovyShell.parse(script);
        }
        return scriptLazy;
    }
}
